package com.apex.wx.service.impl;

import com.apex.common.controller.BaseService;
import com.apex.security.api.entity.Pager;
import com.apex.security.api.entity.TBsdtTechnologicalDao;
import com.apex.wx.domain.ServiceCenterDomain;
import com.apex.wx.service.ServiceCenterService;
import com.jsoniter.output.JsonStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-4-8
 */
@Service
public class ServiceCenterServiceImpl extends BaseService implements ServiceCenterService {

    @Autowired
    private ServiceCenterDomain domain;

    @Override
    public void index(ModelMap modelMap, String userID) {
        Map<String,Object> map=new HashMap<>();
        map.put("userID",userID);
        Pager pager=new Pager();
        pager.setCurrent(1);
        pager.setRows(8);
        map.put("pager",pager);
        /***查询用户订阅服务***/
        List<TBsdtTechnologicalDao> list_dy=null;
        if(!StringUtils.isEmpty(userID)){
            list_dy=getAllListObject(TBsdtTechnologicalDao.class,domain.getAllPageDyByUserId(JsonStream.serialize(map)));
        }
        modelMap.put("list_dy",list_dy);
        /**查询热门服务**/
        List<TBsdtTechnologicalDao> list_hot=getAllListObject(TBsdtTechnologicalDao.class,domain.findAllHotServer(JsonStream.serialize(map)));
        modelMap.put("lsit_hot",list_hot);
        /**查询最新服务**/
        List<TBsdtTechnologicalDao> list_new=getAllListObject(TBsdtTechnologicalDao.class,domain.findAllByPager(JsonStream.serialize(map)));
        modelMap.put("list_new",list_new);
    }

    @Override
    public void entity(ModelMap modelMap, String userID,Long id) {
        modelMap.put("msg",false);
        if(null!=id&&!StringUtils.isEmpty(userID)){
            TBsdtTechnologicalDao technologicalDao=getObject(TBsdtTechnologicalDao.class,domain.getEntityById(userID,id));
            String url=technologicalDao.getUrl();
            if(url.indexOf("http://")==-1){
                technologicalDao.setUrl("http://swzxapp.tec.suda.edu.cn"+url);
            }
            modelMap.put("msg",true);
            modelMap.put("entity",technologicalDao);
        }
    }

    /**
     * 取消订阅
     * @param userID
     * @param id
     * @return
     */
    @Override
    public String entityQx(String userID, Long id) {
        return domain.updateEntityQx(userID,id);
    }

    /**
     * 订阅服务
     * @param userID
     * @param id
     * @return
     */
    @Override
    public String entityDy(String userID, Long id) {
        return domain.updateEntity(userID,id);
    }

    /**
     * 查询全部订阅服务
     * @param modelMap
     * @param userID
     */
    @Override
    public void openUserCollectService(ModelMap modelMap, String userID,String name) {
        List<TBsdtTechnologicalDao> list=null;
        if(!StringUtils.isEmpty(name)&&!StringUtils.isEmpty(userID)){
            Map<String,Object> map=new HashMap<>();
            map.put("userID",userID);
            map.put("name",name);
            list=getAllListObject(TBsdtTechnologicalDao.class,domain.getAllPageDyByUserId(JsonStream.serialize(map)));
        }else if(!StringUtils.isEmpty(userID)){
            list=getAllListObject(TBsdtTechnologicalDao.class,domain.getAllDyByUserId(userID));
        }
        modelMap.put("dataList",list);
        modelMap.put("name",name);
    }

    /**
     * 查询全部服务，服务检索页面
     * @param modelMap
     */
    @Override
    public void findAllService(ModelMap modelMap) {

    }

    /**
     * 条件检索服务
     * @param dao
     * @return
     */
    @Override
    public String findAllService(TBsdtTechnologicalDao dao) {
        List<TBsdtTechnologicalDao> list = null;
        if (null == dao) {
            list = getAllListObject(TBsdtTechnologicalDao.class, domain.getAllList());
        } else {
            String code = JsonStream.serialize(dao);
            list = getAllListObject(TBsdtTechnologicalDao.class, domain.findAllByPager(code));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", true);
        map.put("dataList", list);
        return JsonStream.serialize(map);
    }

    /**
     * app专用
     * @param modelMap
     * @param userID
     */
    @Override
    public void appIndex(ModelMap modelMap, String userID,String name) {
        Map<String,Object> map=new HashMap<>();
        map.put("userID",userID);
        if(!StringUtils.isEmpty(name)){
            map.put("name",name);
        }
        List<TBsdtTechnologicalDao> list=getAllListObject(TBsdtTechnologicalDao.class,domain.findAllByPager(JsonStream.serialize(map)));
        modelMap.put("dataList",getList(list));
        map.put("order","true");
        List<TBsdtTechnologicalDao> list_hot=getAllListObject(TBsdtTechnologicalDao.class,domain.findAllByPager(JsonStream.serialize(map)));
        modelMap.put("hotList",getList(list_hot));
        List<TBsdtTechnologicalDao> list_sc=getAllListObject(TBsdtTechnologicalDao.class,domain.getAllPageDyByUserId(JsonStream.serialize(map)));
        modelMap.put("scList",getList(list_sc));
        modelMap.put("name",name);
    }

    private List<TBsdtTechnologicalDao> getList(List<TBsdtTechnologicalDao> list){
        if(null!=list&&list.size()>0){
            for (TBsdtTechnologicalDao dao:list){
                dao.setFwtp("http://swzxapp.tec.suda.edu.cn"+dao.getFwtp());
            }
        }
        return list;
    }

    /**
     * 查询全部服务
     * @param modelMap
     */
    @Override
    public void findAllProcess(ModelMap modelMap) {
        List<TBsdtTechnologicalDao> list=getAllListObject(TBsdtTechnologicalDao.class,domain.getAllList());
        modelMap.put("dataList",list);
    }

    /**
     *查询场地借用中的教室借用情况
     * @param modelMap
     */
    @Override
    public void findAllJsjyqk(ModelMap modelMap,String jsmc,String sqrq,Pager pager) {


    }

    @Override
    public void findJsjyInfo(ModelMap modelMap, Long id) {

    }

}

